﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace carParking
{

    public partial class frmMain : Form
    {
        private int[] threshold = { 800, 800, 800, 800, 800, 800,800, 800};
        private bool[] statePark = { false, false, false, false, false, false, false, false };
        private bool[] oldStatePark = { false, false, false, false, false, false, false, false };
        private int carInPark = 0;
        private int carEntryNow = 0;
        private int allCarEntry = 0;
        private int count = 0;
        DateTime CurrTime = DateTime.Now;
        private bool isParkEmpty = true;
        private enum state { 
            _headerAddress1_, 
            _headerAddress2_, 
            _commandTag_, 
            ReadSensor0_1, 
            ReadSensor0_2, 
            ReadSensor1_1, 
            ReadSensor1_2, 
            ReadSensor2_1,
            ReadSensor2_2,
            ReadSensor3_1,
            ReadSensor3_2,
            ReadSensor4_1,
            ReadSensor4_2,
            ReadSensor5_1,
            ReadSensor5_2,
            ReadSensor6_1,
            ReadSensor6_2,
            ReadSensor7_1,
            ReadSensor7_2,
            ShowSensor0,
            ShowSensor1,
            ShowSensor2,
            ShowSensor3,
            ShowSensor4,
            ShowSensor5,
            ShowSensor6,
            ShowSensor7,
            ParkCHK
        }
        private enum cmd { READ_SENSOR_0 = 0, READ_SENSOR_1 = 1, READ_SENSOR_2 = 2, READ_SENSOR_3 = 3, READ_SENSOR_4 = 4, READ_SENSOR_5 = 5, READ_SENSOR_6 = 6, READ_SENSOR_7 = 7 }
        private SerialCommunication serialConn;
        private state nextState = state._headerAddress1_;

        public frmMain()
        {
            InitializeComponent();

            btnConnect.Enabled = true;
            btnDisConnect.Enabled = false;
            //sw = new Stopwatch();

            //setup connect with serailport
            serialConn = new SerialCommunication("9600", "None", "One", "8", "COM1");
        }



        private void btnConnect_Click(object sender, EventArgs e)
        {

            serialConn.portName = "COM" +  txtNumPort.Text;
            if (serialConn.openPort())
            {
                status_con.ForeColor = System.Drawing.Color.Green;
                status_con.Text = "CONNECTED";
                btnDisConnect.Enabled = true;
                btnConnect.Enabled = false;
                serialConn.getSerialPort().DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);
            }
            ///
        }

        
        public void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            
            
            //char b = (char)serialConn.getSerialPort().ReadByte();
            int bytes = serialConn.getSerialPort().BytesToRead;
            //create a byte array to hold the awaiting data
            byte[] comBuffer = new byte[bytes];
            //read the data and store it
            serialConn.getSerialPort().Read(comBuffer, 0, bytes);
            //DisplayData(ByteToHex(comBuffer) + "\n");
            for (int i = 0; i < bytes;i++)
            {
                int b = comBuffer[i];
                //DisplayData(b.ToString()+"\n");
                
                switch (nextState)
                {
                    case state._headerAddress1_:
                        if (b == 250)
                        { //board to com 0x46(70) 0xF0(240)
                            
                            nextState = state._headerAddress2_;
                        }
                        else
                        {
                            
                        }
                        break;
                    case state._headerAddress2_:
                        if (b == 84) //board to com 0x46(70) 0xF0(240)
                        {
                            nextState = state._commandTag_;
                        }
                        else
                        {
                            
                            nextState = state._headerAddress1_;
                        }
                        break;
                    case state._commandTag_:
                        switch ((cmd)(b))
                        {
                            case cmd.READ_SENSOR_0:
                                nextState = state.ReadSensor0_1;
                                break;
                            case cmd.READ_SENSOR_1:
                                nextState = state.ReadSensor1_1;
                                break;
                            case cmd.READ_SENSOR_2:
                                nextState = state.ReadSensor2_1;
                                break;
                            case cmd.READ_SENSOR_3:
                                nextState = state.ReadSensor3_1;
                                break;
                            case cmd.READ_SENSOR_4:
                                nextState = state.ReadSensor4_1;
                                break;
                            case cmd.READ_SENSOR_5:
                                nextState = state.ReadSensor5_1;
                                break;
                            case cmd.READ_SENSOR_6:
                                nextState = state.ReadSensor6_1;
                                break;
                            case cmd.READ_SENSOR_7:
                                nextState = state.ReadSensor7_1;
                                break;
                            default:
                                //MessageBox.Show("default");
                                nextState = state._headerAddress1_;
                                break;
                        }
                        break;
// read sensor 0
                    case state.ReadSensor0_1:
                        Rdata[0] = b << 8;
                        nextState = state.ReadSensor0_2;
                        break;
                    case state.ReadSensor0_2:
                        Rdata[0] =Rdata[0] | b;
                        //nextState = state.ShowSensor0;
                        progressBar0.Invoke(new EventHandler(delegate
                        {
                            progressBar0.Value = Rdata[0];
                        }));

                        txtSensor_0.Invoke(new EventHandler(delegate
                        {
                            txtSensor_0.Text = Rdata[0].ToString();
                        }));

        //check park state 0
                        this.oldStatePark[0] = this.statePark[0];
                        this.statePark[0] = Rdata[0] < this.threshold[0] ? true : false;


                        nextState = state.ParkCHK;
                        break;

// read sensor 1
                    case state.ReadSensor1_1:
                        Rdata[1] = b << 8;
                        nextState = state.ReadSensor1_2;
                        break;
                    case state.ReadSensor1_2:
                        Rdata[1] = Rdata[1] | b;
                        progressBar1.Invoke(new EventHandler(delegate
                        {
                            progressBar1.Value = Rdata[1];
                        }));

                        txtSensor_1.Invoke(new EventHandler(delegate
                        {
                            txtSensor_1.Text = Rdata[1].ToString();
                        }));

        //check park 1
                        this.oldStatePark[1] = this.statePark[1];
                        if (Rdata[1] < this.threshold[1])
                        {
                            this.statePark[1] = true;
                            picInpark_1.Invoke(new EventHandler(delegate
                            {
                                picInpark_1.Image = carParking.Properties.Resources.notEmpty;
                            }));
                        }
                        else
                        {
                            this.statePark[1] = false;
                            picInpark_1.Invoke(new EventHandler(delegate
                            {
                                picInpark_1.Image = carParking.Properties.Resources.empty1;
                            }));
                        }

                        nextState = state.ParkCHK;
                        //nextState = state.ShowSensor1;
                        break;

// read sensor 2
                    case state.ReadSensor2_1:
                        Rdata[2] = b << 8;
                        nextState = state.ReadSensor2_2;
                        break;
                    case state.ReadSensor2_2:
                        Rdata[2] = Rdata[2] | b;
                        progressBar2.Invoke(new EventHandler(delegate
                        {
                            progressBar2.Value = Rdata[2];
                        }));

                        txtSensor_2.Invoke(new EventHandler(delegate
                        {
                            txtSensor_2.Text = Rdata[2].ToString();
                        }));

        //check park 2
                        this.oldStatePark[2] = this.statePark[2];
                        if (Rdata[2] < this.threshold[2])
                        {
                            this.statePark[2] = true;
                            picInpark_2.Invoke(new EventHandler(delegate
                            {
                                picInpark_2.Image = carParking.Properties.Resources.notEmpty;
                            }));
                        }
                        else
                        {
                            this.statePark[2] = false;
                            picInpark_2.Invoke(new EventHandler(delegate
                            {
                                picInpark_2.Image = carParking.Properties.Resources.empty1;
                            }));
                        }


                        nextState = state.ParkCHK;
                        //nextState = state.ShowSensor2;
                        break;

// read sensor 3
                    case state.ReadSensor3_1:
                        Rdata[3] = b << 8;
                        nextState = state.ReadSensor3_2;
                        break;
                    case state.ReadSensor3_2:
                        Rdata[3] = Rdata[3] | b;
                        progressBar3.Invoke(new EventHandler(delegate
                        {
                            progressBar3.Value = Rdata[3];
                        }));

                        txtSensor_3.Invoke(new EventHandler(delegate
                        {
                            txtSensor_3.Text = Rdata[3].ToString();
                        }));

        //check park 3
                        this.oldStatePark[3] = this.statePark[3];
                        if (Rdata[3] < this.threshold[3])
                        {
                            this.statePark[3] = true;
                            picInpark_3.Invoke(new EventHandler(delegate
                            {
                                picInpark_3.Image = carParking.Properties.Resources.notEmpty;
                            }));
                        }
                        else
                        {
                            this.statePark[3] = false;
                            picInpark_3.Invoke(new EventHandler(delegate
                            {
                                picInpark_3.Image = carParking.Properties.Resources.empty1;
                            }));
                        }


                        nextState = state.ParkCHK;

                        //nextState = state.ShowSensor3;
                        break;

// read sensor 4
                    case state.ReadSensor4_1:
                        Rdata[4] = b << 8;
                        nextState = state.ReadSensor4_2;
                        break;
                    case state.ReadSensor4_2:
                        Rdata[4] = Rdata[4] | b;
                        //nextState = state.ShowSensor4;
                        progressBar4.Invoke(new EventHandler(delegate
                        {
                            progressBar4.Value = Rdata[4];
                        }));

                        txtSensor_4.Invoke(new EventHandler(delegate
                        {
                            txtSensor_4.Text = Rdata[4].ToString();
                        }));

        //check park 4
                        this.oldStatePark[4] = this.statePark[4];
                        if (Rdata[4] < this.threshold[4])
                        {
                            this.statePark[4] = true;

                            picInpark_4.Invoke(new EventHandler(delegate
                            {
                                picInpark_4.Image = carParking.Properties.Resources.notEmpty;
                            }));
                        }
                        else
                        {
                            this.statePark[4] = false;
                            picInpark_4.Invoke(new EventHandler(delegate
                            {
                                picInpark_4.Image = carParking.Properties.Resources.empty1;
                            }));
                        }

                        nextState = state.ParkCHK;
                        
                        break;

                        
// read sensor 5
                    case state.ReadSensor5_1:
                        Rdata[5] = b << 8;
                        nextState = state.ReadSensor5_2;
                        break;
                    case state.ReadSensor5_2:
                        Rdata[5] = Rdata[5] | b;
                        progressBar5.Invoke(new EventHandler(delegate
                        {
                            progressBar5.Value = Rdata[5];
                        }));

                        txtSensor_5.Invoke(new EventHandler(delegate
                        {
                            txtSensor_5.Text = Rdata[5].ToString();
                        }));

        //check park 5
                        this.oldStatePark[5] = this.statePark[5];
                        if (Rdata[5] < this.threshold[5])
                        {
                            this.statePark[1] = true;
                            picInpark_5.Invoke(new EventHandler(delegate
                            {
                                picInpark_5.Image = carParking.Properties.Resources.notEmpty;
                            }));
                        }
                        else
                        {
                            this.statePark[5] = false;
                            picInpark_5.Invoke(new EventHandler(delegate
                            {
                                picInpark_5.Image = carParking.Properties.Resources.empty1;
                            }));
                        }

                        nextState = state.ParkCHK;    
                        //nextState = state.ShowSensor5;
                        break;

// read sensor 6
                    case state.ReadSensor6_1:
                        Rdata[6] = b << 8;
                        nextState = state.ReadSensor6_2;
                        break;
                    case state.ReadSensor6_2:
                        Rdata[6] = Rdata[6] | b;

                        //nextState = state.ShowSensor6;
                        progressBar6.Invoke(new EventHandler(delegate
                        {
                            progressBar6.Value = Rdata[6];
                        }));

                        txtSensor_6.Invoke(new EventHandler(delegate
                        {
                            txtSensor_6.Text = Rdata[6].ToString();
                        }));

        //check park 6
                        this.oldStatePark[6] = this.statePark[6];
                        if (Rdata[6] < this.threshold[6])
                        {
                            this.statePark[6] = true;
                            picInpark_6.Invoke(new EventHandler(delegate
                            {
                                picInpark_6.Image = carParking.Properties.Resources.notEmpty;
                            }));
                        }
                        else
                        {
                            this.statePark[6] = false;
                            picInpark_6.Invoke(new EventHandler(delegate
                            {
                                picInpark_6.Image = carParking.Properties.Resources.empty1;
                            }));
                        }

                        nextState = state.ParkCHK;
                        break;

// read sensor 7
                    case state.ReadSensor7_1:
                        Rdata[7] = b << 8;
                        nextState = state.ReadSensor7_2;
                        break;
                    case state.ReadSensor7_2:
                        Rdata[7] = Rdata[7] | b;
                        //nextState = state.ShowSensor7;
                        progressBar7.Invoke(new EventHandler(delegate
                        {
                            progressBar7.Value = Rdata[7];
                        }));

                        txtSensor_7.Invoke(new EventHandler(delegate
                        {
                            txtSensor_7.Text = Rdata[7].ToString();
                        }));

        //check park state 7
                        this.oldStatePark[7] = this.statePark[7];
                        this.statePark[7] = Rdata[7] < this.threshold[7] ? true : false;

                        nextState = state.ParkCHK;
                        break;
                   
                    case state.ParkCHK:
                       /*
                        this.isParkEmpty = (this.statePark[1] ||
                            this.statePark[2] ||
                            this.statePark[3] ||
                            this.statePark[4] ||
                            this.statePark[5] ||
                            this.statePark[6]) ? false : true;
                        */
//entran 
                        if (this.statePark[0])
                        {
                            if (!this.oldStatePark[0])
                            {
                                this.carEntryNow++;
                                this.allCarEntry++;
                            }
                        }

//park 1
                        if (this.statePark[1])
                        {
                            if (!this.oldStatePark[1])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox1.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox1.AppendText("ลานจอดที่ 1  ");
                                        richTextBox1.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox1.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark++;
                            }
                        }
                        else
                        {
                            if (this.oldStatePark[1])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox2.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox2.AppendText("ลานจอดที่ 1  ");
                                        richTextBox2.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox2.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark--;
                                this.carEntryNow--;

                            }
                        }

//park 2
                        if (this.statePark[2])
                        {
                            if (!this.oldStatePark[2])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox1.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox1.AppendText("ลานจอดที่ 2  ");
                                        richTextBox1.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox1.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark++;
                            }
                        }
                        else
                        {
                            if (this.oldStatePark[2])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox2.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox2.AppendText("ลานจอดที่ 2  ");
                                        richTextBox2.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox2.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark--;
                                this.carEntryNow--;
                            }
                        }
//park 3
                        if (this.statePark[3])
                        {
                            if (!this.oldStatePark[3])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox1.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox1.AppendText("ลานจอดที่ 3  ");
                                        richTextBox1.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox1.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark++;
                            }
                        }
                        else
                        {
                            if (this.oldStatePark[3])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox2.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox2.AppendText("ลานจอดที่ 3  ");
                                        richTextBox2.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox2.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark--;
                                this.carEntryNow--;
                            }
                        }

//park 4
                        if (this.statePark[4])
                        {
                            if (!this.oldStatePark[4])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox1.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox1.AppendText("ลานจอดที่ 3  ");
                                        richTextBox1.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox1.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark++;
                            }
                        }
                        else
                        {
                            if (this.oldStatePark[4])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox2.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox2.AppendText("ลานจอดที่ 4  ");
                                        richTextBox2.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox2.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark--;
                                this.carEntryNow--;
                            }
                        }

//park 5
                        if (this.statePark[5])
                        {
                            if (!this.oldStatePark[5])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox1.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox1.AppendText("ลานจอดที่ 5  ");
                                        richTextBox1.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox1.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark++;
                            }
                        }
                        else
                        {
                            if (this.oldStatePark[5])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox2.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox2.AppendText("ลานจอดที่ 5  ");
                                        richTextBox2.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox2.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark--;
                                this.carEntryNow--;
                            }
                        }

//park 6
                        if (this.statePark[6])
                        {
                            if (!this.oldStatePark[6])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox1.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox1.AppendText("ลานจอดที่ 6  ");
                                        richTextBox1.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox1.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark++;
                            }
                        }
                        else
                        {
                            if (this.oldStatePark[6])
                            {
                                if (this.count > 6)
                                {
                                    richTextBox2.Invoke(new EventHandler(delegate
                                    {
                                        richTextBox2.AppendText("ลานจอดที่ 6  ");
                                        richTextBox2.AppendText(CurrTime.DayOfWeek.ToString() + "  ");
                                        richTextBox2.AppendText(CurrTime.TimeOfDay + "\n");
                                    }));
                                    this.count = 0;
                                }
                                this.count++;
                                this.carInPark--;
                                this.carEntryNow--;
                            }
                        }


                        if (this.carInPark < 0)
                            this.carInPark = 0;
                        if (this.carEntryNow < 0)
                            this.carEntryNow = 0;

                        label15.Invoke(new EventHandler(delegate
                        {
                            label15.Text = ((this.allCarEntry) / 8).ToString();
                        }));
                       

                        textBox2.Invoke(new EventHandler(delegate
                        {
                            textBox2.Text = ((this.carEntryNow - this.carInPark)/8).ToString();
                        }));


                        i--;
                        nextState = state._headerAddress1_;
                        break;
                    default:
                        
                        nextState = state._headerAddress1_;
                        break;
                }
            }
        }

        private void btnDisConnect_Click(object sender, EventArgs e)
        {
            if (serialConn.closePort())
            {
                btnDisConnect.Enabled = false;
                btnConnect.Enabled = true;
                status_con.ForeColor = System.Drawing.Color.Red;
                status_con.Text = "NOT CONNECT";
            }
        }

        private void trackBar0_Scroll(object sender, EventArgs e)
        {
            this.threshold[0] = trackBar0.Value;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            this.threshold[1] = trackBar1.Value;
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            this.threshold[2] = trackBar2.Value;
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            this.threshold[3] = trackBar3.Value;
        }

        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            this.threshold[4] = trackBar4.Value;
        }

        private void trackBar5_Scroll(object sender, EventArgs e)
        {
            this.threshold[5] = trackBar5.Value;
        }

        private void trackBar6_Scroll(object sender, EventArgs e)
        {
            this.threshold[6] = trackBar6.Value;
        }

        private void trackBar7_Scroll(object sender, EventArgs e)
        {
            this.threshold[7] = trackBar7.Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox2.Text = "";
        }


    }
}
